using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public static class GameUtility
{
    public const string EditorOutPutPath = "../../AssetBundles/";
    public const string EditorAssetBundlePath = "Bundles/";
    public const string EditorLuaPath = "Lua";
    public const string OutPutLuaVersionFile = "LuaVersion.json";
    public const string EditorShaderPath = "Shader";
    public const string BundleExt = "unity3d";
    public static bool CheckExtensions(string sourceExtension, string targetExtensions)
    {
        var Extensions = targetExtensions.Split(',');
        return Extensions.Contains(sourceExtension);
    }

    public static void IfExitsCreate(string sourcePath)
    {
        if (!Directory.Exists(sourcePath))
        {
            Directory.CreateDirectory(sourcePath);
        }
    }
    public static string GetRelativePath(string sourcePath, string RelativePath)
    {
        var sourceReleace = sourcePath.Replace("\\", "/");
        var sourceRelativePath = RelativePath.Replace("\\", "/");
        if (sourceReleace.Length == sourceRelativePath.Length)
        {
            return "";
        }
        return sourceReleace.Substring(sourceRelativePath.Length);
    }

    public static void AlterPath(string buildPath ,Action<FileInfo> onAlterAction)
    {
        DirectoryInfo rootPath = new DirectoryInfo(buildPath);
        if (!rootPath.Exists)
        {
            Debug.LogError($"path : {rootPath.FullName} is not found !!!!!");
            return;
        }

        Queue<DirectoryInfo> foreachPath = new Queue<DirectoryInfo>();
        foreachPath.Enqueue(rootPath);
        do
        {
            var curDirectory = foreachPath.Dequeue();
            var allFiles = curDirectory.GetFiles("*");
            if (allFiles.Length > 0)
            {
                foreach (var fileInfo in allFiles)
                {
                    
                    if (onAlterAction != null)
                    {
                        onAlterAction(fileInfo);
                    }
                }
            }

            var childDirectories = curDirectory.GetDirectories();
            foreach (var child in childDirectories)
            {
                if (child.Name.StartsWith("."))
                {
                    continue;
                }
                foreachPath.Enqueue(child);
            }
        } while (foreachPath.Count != 0);
    }
    
    public static void CopyDire2Target(string sourcePath, string destPath)
    {
        if (Directory.Exists(sourcePath))
        {
            if (!Directory.Exists(destPath))
            {
                //目标目录不存在则创建
                try
                {
                    Directory.CreateDirectory(destPath);
                }
                catch (Exception ex)
                {
                    throw new Exception("创建目标目录失败：" + ex.Message);
                }
            }
            //获得源文件下所有文件
            List<string> files = new List<string>(Directory.GetFiles(sourcePath));
            files.ForEach(c =>
            {
                if (c.StartsWith("."))
                {
                    return;
                }
                string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
                File.Copy(c, destFile, true);//覆盖模式
            });
            //获得源文件下所有目录文件
            List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));
            folders.ForEach(c =>
            {
                if (c.StartsWith("."))
                {
                    return;
                }
                string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
                //采用递归的方法实现
                CopyDire2Target(c, destDir);
            });
 
        }
        
        // Directory(sourceParh, targetPath + "/MyFile.text", true);
    }
}